package com.r2coding.ent.service.impl;

import com.github.pagehelper.PageHelper;
import com.r2coding.ent.util.CommonUtil;
import com.r2coding.ent.vo.req.GetAllPictureReqVo;
import com.r2coding.ent.vo.req.MovePictureReqVo;
import com.r2coding.ent.vo.resp.GetAllPictureRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Query;

import com.r2coding.ent.dao.PictureDao;
import com.r2coding.ent.entity.PictureEntity;
import com.r2coding.ent.service.PictureService;
import org.springframework.util.CollectionUtils;

/**
 * 图片
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-06-27 21:53:54
 */
@Service("pictureService")
public class PictureServiceImpl extends ServiceImpl<PictureDao, PictureEntity> implements PictureService {

    @Autowired
    CommonUtil commonUtil;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PictureEntity> page = this.page(
                new Query<PictureEntity>().getPage(params),
                new QueryWrapper<PictureEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public GetAllPictureRespVo getAll(GetAllPictureReqVo req) {
        GetAllPictureRespVo respVo = new GetAllPictureRespVo();
        QueryWrapper<PictureEntity> wrapper = new QueryWrapper<PictureEntity>().eq("album_id", req.getAlbumId());
        Integer count = this.baseMapper.selectCount(wrapper);
        respVo.setTotal(count);
        PageHelper.startPage(req.getCurrent(), req.getSize());
        List<PictureEntity> list = this.baseMapper.selectList(wrapper);
        respVo.setPictureList(list);
        return respVo;
    }

    @Override
    public void movePicture(MovePictureReqVo req) {
        if (!CollectionUtils.isEmpty(req.getPictureIds())) {
            req.getPictureIds().forEach( item -> {
                commonUtil.checkAlbumSize(req.getAlbumId());
                PictureEntity picture = new PictureEntity();
                picture.setAlbumId(req.getAlbumId());
                picture.setId(item);
                this.baseMapper.updateById(picture);
            });
        }
    }
}